package edgeEntity

import (
	"encoding/json"
	"gitee.com/fierce_wolf/go-fox-edge-common/commEntity"
)

type PeriodTaskEntity struct {
	BaseEntity *commEntity.BaseEntity // 基本信息

	TaskName     string // 模板名称
	Manufacturer string // 设备厂商名称
	DeviceType   string // 设备类型名

	SelectDevice bool
	DeviceIds    []int64 // 设备ID
	ObjectIds    []string
	TaskParam    map[string]interface{} // 任务参数
}

type periodTaskPo struct {
	Id           int64                  `json:"id" db:"id"`                      // 对象id
	CreateTime   int64                  `json:"createTime" db:"create_time"`     // 创建时间
	UpdateTime   int64                  `json:"updateTime" db:"update_time"`     // 更新时间
	Manufacturer string                 `json:"manufacturer" db:"manufacturer"`  // 设备厂商名称
	DeviceType   string                 `json:"deviceType" db:"device_type"`     // 设备类型名
	TaskName     string                 `json:"taskName" db:"task_name"`         // 任务名称
	SelectDevice bool                   `json:"selectDevice" db:"select_device"` // 设备名称
	DeviceIds    []int64                `json:"deviceIds" db:"device_ids"`       // 扩展参数
	ObjectIds    []string               `json:"objectIds" db:"object_ids"`       // 扩展参数
	TaskParam    map[string]interface{} `json:"taskParam" db:"task_param"`       // 通道名称
}

func (e *PeriodTaskEntity) Type() string {
	return "PeriodTaskEntity"
}

func (e *PeriodTaskEntity) TableName() string {
	return "tb_period_task"
}

func (e *PeriodTaskEntity) GetBaseEntity() *commEntity.BaseEntity {
	return e.BaseEntity
}

func (e *PeriodTaskEntity) Instance() *PeriodTaskEntity {
	return &PeriodTaskEntity{
		BaseEntity: new(commEntity.BaseEntity),
		DeviceIds:  make([]int64, 0),
		ObjectIds:  make([]string, 0),
		TaskParam:  make(map[string]interface{}),
	}
}

func (e *PeriodTaskEntity) NewEntity() commEntity.IEntity {
	return e.Instance()
}

func (e *PeriodTaskEntity) NewPo() interface{} {
	return &periodTaskPo{
		DeviceIds: make([]int64, 0),
	}
}

func (e *PeriodTaskEntity) MakeServiceKey() string {
	p := e.MakeServiceKeyList()
	return commEntity.MakeServiceKey(p)
}

func (e *PeriodTaskEntity) MakeServiceKeyList() []string {
	var p []string
	p = append(p, e.TaskName)
	return p
}

func (e *PeriodTaskEntity) BuildByJson(jsn *string) error {
	var po periodTaskPo
	err := json.Unmarshal([]byte(*jsn), &po)
	if err != nil {
		return err
	}

	e.BuildByPo(&po)
	return nil
}

func (e *PeriodTaskEntity) BuildToJson() (string, error) {
	po := e.BuildToPo()
	code, err := json.Marshal(po)
	if err != nil {
		return "", err
	}

	return string(code), nil
}

func (e *PeriodTaskEntity) BuildByPo(pop interface{}) {
	ptr := e
	po := pop.(*periodTaskPo)

	ptr.BaseEntity.Id = po.Id
	ptr.BaseEntity.CreateTime = po.CreateTime
	ptr.BaseEntity.UpdateTime = po.UpdateTime
	ptr.Manufacturer = po.Manufacturer
	ptr.DeviceType = po.DeviceType
	ptr.TaskName = po.TaskName
	ptr.TaskParam = po.TaskParam
	ptr.SelectDevice = po.SelectDevice
	ptr.DeviceIds = po.DeviceIds
	ptr.ObjectIds = po.ObjectIds
}

func (e *PeriodTaskEntity) BuildToPo() interface{} {
	ptr := e
	po := periodTaskPo{}

	po.Id = ptr.BaseEntity.Id
	po.CreateTime = ptr.BaseEntity.CreateTime
	po.UpdateTime = ptr.BaseEntity.UpdateTime
	po.Manufacturer = ptr.Manufacturer
	po.DeviceType = ptr.DeviceType
	po.TaskName = ptr.TaskName
	po.TaskParam = ptr.TaskParam
	po.SelectDevice = ptr.SelectDevice
	po.DeviceIds = ptr.DeviceIds
	po.ObjectIds = ptr.ObjectIds

	return &po
}
